package org.tankwar.model;

import org.tankwar.util.GlobalUtil;
import org.tankwar.util.ImageUtil;
import org.tankwar.view.GamePanel;

import java.awt.*;

public class Home extends StaticGameComponent{
    private boolean live = true;
    public Home(int x, int y, GamePanel tc) {// 构造函数，传递Home的参数并赋值
        componentImage = GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.HOME_IMAGE_URL));
        this.x = x;
        this.y = y;
        this.width = 44;
        this.height = 44;
        this.gamePanel = tc; // 获得控制
    }
    public void gameOver(Graphics g) { // 处理游戏结束的方法
        gamePanel.tanks.clear();// 作清理页面工作
        gamePanel.metalWall.clear();
        gamePanel.otherWall.clear();
        gamePanel.bombTanks.clear();
        gamePanel.theRiver.clear();
        gamePanel.trees.clear();
        gamePanel.bullets.clear();
        gamePanel.homeWall.clear();
        gamePanel.homeTanks.clear();
        gamePanel.printable = false;
        gamePanel.blood.setLive(false);
        Color c = g.getColor(); // 保存原来的颜色
        g.setColor(Color.green);
        Font f = g.getFont(); // 保存原来的字体
        g.setFont(new Font("微软雅黑", Font.PLAIN, 40));
        g.drawString("你输了！", 220, 250);
        g.drawString("  游戏结束！ ", 220, 300);
        g.setFont(f);
        g.setColor(c);
    }

    @Override
    public void draw(Graphics g) {
        if (live) { // 如果活着，则画出home
            g.drawImage(componentImage, x, y, null);
            for (int i = 0; i < gamePanel.homeWall.size(); i++) {
                BrickWall w = gamePanel.homeWall.get(i);
                w.draw(g);
            }
        } else {
            gameOver(g); // 调用游戏结束
        }
    }
    public boolean isLive() { // 判读是否还活着
        return live;
    }

    public void setLive(boolean live) { // 设置是否存活
        this.live = live;
    }
}
